<?php

namespace app\manage\controller;

use core\libs\db;
use core\libs\input;

class content extends base {
    // 初始化
    public function __init() {
        $this->input = input::request(
            'id', 'action', 'mid', 
            'search_key', 'search_cid', 'search_state', 
            'con_link_cid', 
            'con_link_id',
            'con_file',
            'con_video',
            'con_pic',
            'con_image',
            'con_name', 
            'con_path', 
            'con_alias',  
            'con_time', 
            'con_source', 
            'con_cid',
            'con_content',
            'con_description',
            'con_seo_title',
            'con_seo_keywords',
            'con_seo_description',
            'con_url',
            'con_tpl',
            'con_top',
            'con_state',
            'con_order',
            'con_hot',
            'con_type',
            'con_global'
        );

        // 是否有权限
        $this->is_level($this->baseClass . "?mid={$this->input['mid']}_list");
        if(in_array($this->baseAction, ["submit", "delete", "batch"])) {
            $this->is_level($this->baseClass . "?mid={$this->input['mid']}_edit");
        }
    }
    // 列表
    public function index() {
        extract($this->input);

        $filter = '';	
        if($search_cid != '') {
            $filter .= " and con_cid = '$search_cid'";
        }
        
        if($search_state != '') {
            $filter .= " and con_state = '$search_state'";
        }
        if($search_key) {
            $filter.=" and (con_name like '%$search_key%' or con_alias like '%$search_key%' or con_description like '%$search_key%')";
        }

        $sql = "con_cid = category_id  and (category_id = '$mid' or category_id in (". $this->get_category_ids($mid) .")) $filter";
        $page = new \core\libs\page((int)db::getCount("content, category", $sql), 10);
        $page->args = $this->getArgs("", 1);
        $result = db::fetchAll(
            "content, category", 
            "$sql order by con_top desc, con_hot desc, con_order desc, con_link_id, con_id desc" . $page->limit
        );

        // 获取后台模板
        $res_tpl = $this->get_tpl();

        $this->assign("search", [
            'cid' => $search_cid,
            'state' => $search_state,
            'key' => $search_key
        ]);
        $this->assign("global", [
            "page" => $page,
            "mid" => $mid,
            "res_tpl" => $res_tpl,
        ]);
        $this->assign("result", $result);
        $this->display($res_tpl["as_tpl"]["list"]);
    }
    // 编辑
    public function edit() {
        extract($this->input);

        // 获取后台模板
        $res_tpl = $this->get_tpl();

        $this->assign("row", db::fetch("content", "con_id = '$id'"));
        $this->assign("global", [
            "id" => $id,
            "mid" => $mid,
            "res_tpl" => $res_tpl,
        ]);
        $this->display($res_tpl["as_tpl"]["add"]);
    }
    // 添加、更新
    public function submit() {
        extract($this->input);

        //预处理数据
		$form_datas = array(
			'con_link_cid' => $con_link_cid ? implode(',', $con_link_cid) : "",
			'con_link_id' => $con_link_id ? implode(',', $con_link_id) : "",
			'con_name' => $con_name, 
			'con_path' => $con_path, 
			'con_alias' => $con_alias,
			'con_source' => $con_source, 
			'con_cid' => (int)$con_cid,
			'con_content' => $con_content,
			'con_state' => (int)$con_state,
			'con_url' => $con_url,
			'con_top' => (int)$con_top,
			'con_pic' => $con_pic ? implode(",", $con_pic) : "",
			'con_file' => $con_file ? implode(",", $con_file) : "",
			'con_video' => $con_video ? implode(",", $con_video) : "",
            'con_image' => $con_image,
			'con_description' => $con_description,
			'con_seo_title' => $con_seo_title,
			'con_seo_keywords' => $con_seo_keywords,
			'con_seo_description' => $con_seo_description,
			'con_tpl' => $con_tpl,
			'con_order' => (int)$con_order,
			'con_hot' => (int)$con_hot,
            'con_type' => (int)$con_type,
			'con_global' => $con_global ? implode("||", $con_global) : "", 
			'con_time' => (int)strtotime($con_time)
		);
        foreach($form_datas as $key => $val) {
            $form_datas[$key] = html_entity_decode($val);
        }

        if(empty($id)){
			//添加内容
			db::insert("content", $form_datas);	
			alert('内容添加成功!', U($this->baseClass, $this->getArgs()));		
		}
		else{
			//更新	
			db::update("content", $form_datas, "con_id = '$id'");
			alert('内容更新成功!', U($this->baseClass, $this->getArgs()));
		}
    }
    // 删除
    public function delete() {
        extract($this->input);

        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        //删除内容
		db::delete('content', "con_id in ($id)");

        alert('删除成功!', U($this->baseClass, $this->getArgs()));		
    }
    // 批量操作
    public function batch() {
        extract($this->input);

        if(!$action) alert('请选择要操作的事件!', U($this->baseClass, $this->getArgs()));
        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        // 审核
        if($action == "state_yes" || $action == "state_no") {
            //更新	
            $state = array("state_yes" => 1, "state_no" =>0);
            db::update(
                "content", array('con_state' => $state[$action]), "con_id in ($id)"
            );
            alert('更新成功!', U($this->baseClass, $this->getArgs()));		
        }

        // 删除
        if($action == "delete") {
            $this->delete();
        }
    }
    // 获取后台模板
    public function get_tpl() {
        extract($this->input);

        // 获取后台模板
        $res_tpl = db::fetch(
            "category", "category_id = '$mid'", "category_admin_templates, category_name"
        );
        if($res_tpl['category_admin_templates']) {
            $admin_tpl = explode(',', $res_tpl['category_admin_templates']);
            $res_tpl['as_tpl'] = array(
                "add" => $admin_tpl[0], 
                "list" => $admin_tpl[1]
            );
        }
        else {
            // 默认模板
            $res_tpl['as_tpl'] = array(
                "add" => "content_add", 
                "list" => "content_list"
            );
        }

        return $res_tpl;
    }
    // 获取固定地址栏参数
    protected function getArgs($args = "", $type = 0) {
        extract($this->input);
        $baseurl = "?search_cid={$search_cid}&search_state={$search_state}&search_key={$search_key}&mid={$mid}{$args}";
        return $this->get_base_args($baseurl, $type);
    }
}